package com.example.f8399.testdemo.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.bean.TempBean;
import com.example.f8399.testdemo.databinding.ActivityRxFirstBinding;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


public class RxFirstActivity extends BaseActivity implements View.OnClickListener {

    ActivityRxFirstBinding mBinding;
    private Disposable mDisposable;
    private List<TempBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_rx_first);

        mBinding.button38.setOnClickListener(this);

        list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TempBean bean = new TempBean("li" + i, i * 2);
            list.add(bean);
        }
    }

    public void showFirst(View view) {

        //虽然我执行了25次的打印，但是我只是在oncomplete中做了最后一次的监听修改
        mDisposable = getChange().subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        showSecond(s);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        LogUtils.d("完成了啊");
                        mBinding.button37.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                        mBinding.button37.setText("完成了啊");
                    }
                });
    }

    public void showSecond(String txt) {
        LogUtils.d(txt);
    }

    public void showThird() {

        mDisposable = Observable.fromIterable(list)

                .flatMap(new Function<TempBean, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(final TempBean tempBean) throws Exception {
                        return Observable.create(new ObservableOnSubscribe<String>() {
                            @Override
                            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                                emitter.onNext(tempBean.getName());
                            }
                        });
                    }
                })

                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object s) throws Exception {
                        LogUtils.d(s);
                    }
                })
        ;

    }

    public void showFour(View view) {
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                for (int i = 0; i < 10; i++) {

                    emitter.onNext("item" + i);
                }
            }
        });

        observable.subscribeOn(Schedulers.io())
                .map(new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) throws Exception {
                        String substring = s.substring(4);
                        return Integer.parseInt(substring);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        LogUtils.d(integer);
                    }
                });
    }

    public void showFive(View view) {
        Observable.fromIterable(list)
        .subscribeOn(Schedulers.io())
        .flatMap(new Function<TempBean, ObservableSource<Integer>>() {
            @Override
            public ObservableSource<Integer> apply(final TempBean tempBean) throws Exception {
                return Observable.create(new ObservableOnSubscribe<Integer>() {
                    @Override
                    public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                        emitter.onNext(tempBean.getAge());
                    }
                });
            }
        })
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                LogUtils.d(integer);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        }, new Action() {
            @Override
            public void run() throws Exception {

            }
        })

        ;
    }
    private Observable<String> getChange() {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                for (int i = 0; i < 25; i++) {

                    emitter.onNext(String.valueOf(i));
                }
                emitter.onComplete();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null) {

            if (!mDisposable.isDisposed()) {
                mDisposable.dispose();
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button38:
                showThird();
                break;
        }
    }
}
